package com.venutech.gongan.api;

import com.alibaba.fastjson.JSONObject;
import com.shihang.misc.date.DateTimeUtil;
import com.venutech.gongan.common.Constants;
import com.venutech.gongan.entity.DictData;
import com.venutech.gongan.entity.Qdsxj;
import com.venutech.gongan.service.ApiRedisService;
import com.venutech.gongan.service.DictDataService;
import com.venutech.gongan.service.EquipmenService;
import com.venutech.gongan.service.QdsxjService;
import com.venutech.gongan.vo.DevicesQueryOptionsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

@RestController
@RequestMapping("devices")
@Api(tags = "设备地图")
public class DevicesQueryController extends AbstractControllerBase {

    Logger logger = LoggerFactory.getLogger(DevicesQueryController.class);

    @Autowired
    private ApiRedisService apiRedisService;
    @Autowired
    QdsxjService qdsxjService;

    @Autowired
    DictDataService dictDataService;


    @ApiOperation("设备地图选项信息")
    @GetMapping("options")
    public DevicesQueryOptionsVO options() {
        Map<String, Object> result = new HashMap<>();
        //摄像机类型
        List<DictData> sxjlxList =dictDataService.getSelsxjlx();
        //维护单位
        List<Qdsxj> unitjsList =qdsxjService.getQdsxYwdw();
        return new DevicesQueryOptionsVO(sxjlxList, unitjsList);
    }


    /**
     * 获取设备在线
     *
     * @param sb_name
     * @param sb_type
     * @return
     * @throws InterruptedException
     */
    @GetMapping("list")
    @ApiOperation("前端设备获取")
    public JSONObject getDevicesByDateTime(@RequestParam(value = "sb_name", required = false) String sb_name,
                                           @RequestParam(value = "sb_type", required = false) String sb_type,
                                           @RequestParam(value = "sb_ywdw", required = false) String sb_ywdw) {
        JSONObject json = new JSONObject();
        List<Qdsxj> list_qdsxj = qdsxjService.getQdsxjBySbNameAndTypeList(sb_name, sb_type, sb_ywdw);
        //设备在线率
        DecimalFormat nformat = new DecimalFormat();
        nformat.applyPattern("0.00%");
        double size = list_qdsxj.size();
        double price;
        if (size > 1) {
//            double count=qdsxjService.getOnlineRate();//获取不在线设备
            int onlineCount = 0;
            for (Qdsxj q : list_qdsxj) {
                if ("1".equals(q.getSb_state())) {
                    onlineCount++;
                }
            }
            price = (size - onlineCount) / size;
        } else if (size == 1) {
            price = 1 / size;
        } else {
            price = 0.00;
        }
        //完好率
        double count = qdsxjService.getPerfactRate();//获取设备状态为1,2
        if (CollectionUtils.isEmpty(list_qdsxj)) {
            json.put("code", 200);
            json.put("data", list_qdsxj);
            json.put("msg", "没有数据,重新查询......");
        } else {
            json.put("code", 200);
            json.put("data", list_qdsxj);
            json.put("onlinerate", nformat.format(price));
            json.put("perfactrate", nformat.format(count));
            json.put("total", list_qdsxj.size());
            apiRedisService.set("onlinerate_在线率", nformat.format(price));
            apiRedisService.set("perfactrate_完好率", nformat.format(count));
            json.put("msg", "查询成功");
        }
        String message = String.format("前端设备获取:设备名称:%s", sb_name);
        String opCondition = String.format("前端设备获取:设备名称:%s", sb_name);

        asyncSaveLog(Constants.AUDIT_LOG_OP_TYPE_QUERY, Constants.QUERY, opCondition, message);
        return json;
    }

}
